# @time     :2022/2/19
# @Author   :Simple
# @FileName :run
"""
测试执行入口
"""
import os

import click
import pytest

from config import ios_cases_path, android_cases_path
from util.device import init_device_info, check_device
from util.logger import log as logger
from util.process import open_process_pool
from util.report import init_report


@click.command()
@click.option('-m', default="run", help='输入运行模式：run 或 debug.')
def run(m):
    """
    执行测试
    （开始前去config.py修改日志类型：调试为DEBUG、测试为INFO）
    """
    # start_clean_file()   # 还未完成。。。
    device_infos = init_device_info()
    device_infos_len = len(device_infos)

    # if m is None or m == "run":
    open_process_pool(func=run_pro, parm=device_infos, num=device_infos_len)
    # elif m == "debug":
    #     open_process_pool(func=run_debug, parm=device_infos, num=device_infos_len)


def run_pro(device_info):
    """
    回归模式
    """
    rerun = '0'
    logger.info("回归模式，开始执行✈✈！")

    device_name = device_info['device_name']
    logger.info(f"run device is {device_name}")
    report = init_report(device_name)
    results_report = os.path.join(report, "results")
    html_report = os.path.join(report, "html")
    cases_path = get_case_path(device_name)
    logger.info(f"testcase path is {cases_path}")

    pytest.main([f"--cmdopt={device_info}", f"--device_opt={device_info}",
                 "-s", "-v",
                 "--alluredir=" + results_report,
                 cases_path,
                 "--reruns", rerun])
    allure_report = f"allure generate {results_report} -c -o {html_report}"
    logger.info(allure_report)
    os.popen(allure_report)
    logger.info("运行结束，生成测试报告♥❤！")


def get_case_path(device_name):
    """
    获取测试用例文件
    """

    if check_device(device_name) == "ios":
        # ios运行测试用例的目录或文件 (还未完成iOS的测试用例！！！)
        cases_path = ios_cases_path
    else:
        # android运行测试用例的目录或文件
        cases_path = android_cases_path
    logger.info(f"test case path is {cases_path}")
    return cases_path


if __name__ == "__main__":
    run()

